package zkk.com.mengqu.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;
import java.util.Random;

import zkk.com.mengqu.R;
import zkk.com.mengqu.activity.KitchenActivity;
import zkk.com.mengqu.models.KitchenDetail;

/**
 * 首页厨房列表适配器
 */
public class NearByKitchenAdapter extends BaseAdapter {
    private List<KitchenDetail> lists;
    private Context context;

    public NearByKitchenAdapter(Context context, List<KitchenDetail> liveBeens) {
        this.context = context;
        this.lists = liveBeens;
    }

    @Override
    public int getCount() {
        return lists.size();
    }

    @Override
    public Object getItem(int position) {
        return lists.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if(convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.item_kitchen,null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        holder.linear = setImage(context,holder.linear);
        holder.kitchenName.setText(""+lists.get(position).getName());
        holder.kitchenScore.setText(""+lists.get(position).getScore());
        holder.kitchenSales.setText(""+lists.get(position).getSeles());
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.w("kid",""+ lists.get(position).getId());
                Intent intent = new Intent(context , KitchenActivity.class);
                intent.putExtra("kid",lists.get(position).getId());
                context.startActivity(intent);
            }
        });

        return convertView;
    }
    int[] imageViewss = new int[]{R.drawable.dish15,R.drawable.dish1,R.drawable.dish2,R.drawable.dish3,R.drawable.dish4,R.drawable.dish5,R.drawable.dish6,
            R.drawable.dish7,R.drawable.dish8,R.drawable.dish9,R.drawable.dish10,R.drawable.dish11,R.drawable.dish12,R.drawable.dish13,R.drawable.dish14,};
    int pictureId = 1 ;

    public LinearLayout setImage(Context context, LinearLayout layout){
//        ViewGroup group = (ViewGroup) view.findViewById(R.id.linear);
        layout.removeAllViews();
        ImageView[] imageViews = new ImageView[5];

        for (int i = 0; i < imageViews.length; i++) {
            ImageView imageView = new ImageView(context);

            imageView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));

//            if(i%2 != 0 ){
//                imageViews[i] = imageView;
//                imageView.setImageResource(R.drawable.dish11);
//            }else{
//                imageViews[i] = imageView;
//                imageView.setImageResource(R.drawable.dish1);
//            }

            imageViews[i] = imageView;
            System.out.println("+++++++++++:::"+new Random().nextInt(15)+1);
            pictureId = new Random().nextInt(14)+1;
            System.out.println("+++++++++++22222222 :::::::"+pictureId);
            imageView.setImageResource(imageViewss[pictureId]);
            imageView.setPadding(30,10,0,10);
            layout.addView(imageView);
        }
        return layout;
    }

    class ViewHolder{
        private LinearLayout linear;
        private TextView kitchenName;
        private TextView kitchenScore;
        private TextView kitchenSales;
        public ViewHolder(View view){
            linear = (LinearLayout)view.findViewById(R.id.linear);
            kitchenName = (TextView)view.findViewById(R.id.tx_kitchen_name);
            kitchenScore = (TextView)view.findViewById(R.id.tx_kitchen_score);
            kitchenSales = (TextView)view.findViewById(R.id.tx_kitchen_sales);
        }
    }
}